﻿import 'package:get/get.dart';
import 'package:azring/core/repository/default_api.dart';
import 'package:azring/services/device_sn_manager.dart';
import 'dart:developer' as developer;

class WarrantyStatusLogic extends GetxController {
  final color = ''.obs;
  final size = ''.obs;
  final activationDate = ''.obs;
  final warrantyExpireDate = ''.obs;
  final macAddress = ''.obs;

  final _api = DefaultApi();
  final _deviceSnManager = DeviceSnManager.instance;

  @override
  void onInit() {
    super.onInit();
    loadWarrantyStatus();
  }

  /// 加载保修状态
  Future<void> loadWarrantyStatus() async {
    try {
      // 获取设备序列号
      final deviceSn = _deviceSnManager.getDeviceSn();
      if (deviceSn.isEmpty) {
        developer.log('⚠️ 未找到设备序列号', name: 'WarrantyStatusLogic');
        return;
      }

      // 调用API获取保修状态信息
      final response = await _api.queryWarrantyStatusGet<Map<String, dynamic>>(
        deviceSn: deviceSn,
        isShowLoading: true,
      );

      if (response != null && response['code'] == 200) {
        final data = response['data'] as Map<String, dynamic>;
        
        // 更新UI数据
        color.value = data['deviceColor'] ?? '';
        size.value = data['deviceSize'] ?? '';
        activationDate.value = data['activationDate'] ?? '';
        warrantyExpireDate.value = data['expirationDate'] ?? '';
        macAddress.value = data['macAddress'] ?? '';
        
        developer.log('✅ 保修状态加载成功', name: 'WarrantyStatusLogic');
      } else {
        developer.log('❌ 保修状态加载失败: ${response?['msg']}', name: 'WarrantyStatusLogic');
      }
    } catch (e) {
      developer.log('❌ 加载保修状态异常: $e', name: 'WarrantyStatusLogic');
    }
  }
}
